package com.example.treasureadmin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.treasureadmin.mapper.UserMapper;
import com.example.treasureadmin.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public List<UserModel> getAll(){
        QueryWrapper<UserModel> queryWrapper = new QueryWrapper<>();
        return userMapper.selectList(queryWrapper);
    }

    //查找指定用户
    public UserModel userFind(String email){
        QueryWrapper<UserModel> userFindQueryWrapper = new QueryWrapper<>();
        userFindQueryWrapper.eq("email", email);
        UserModel userModel = userMapper.selectOne(userFindQueryWrapper);
        if (userModel == null){
            return null;
        }
        return userMapper.selectOne(userFindQueryWrapper);
    }
    //修改密码
    public String changePassword(String email,String password) {
        QueryWrapper<UserModel> passwordQueryWrapper = new QueryWrapper<>();
        passwordQueryWrapper.eq("email", email);
        UserModel userModel = userMapper.selectOne(passwordQueryWrapper);
        if(userModel == null)
        {
            return "101";
        }
        UpdateWrapper<UserModel> userModelUpdateWrapper = new UpdateWrapper<>();
        userModelUpdateWrapper.eq("email",email).set("password",password);
        userMapper.update(null,userModelUpdateWrapper);
        return "200";
    }
    //注销
    public String logOut(String email){
        UpdateWrapper<UserModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("email",email).set("is_valid",0);
        userMapper.update(null,updateWrapper);
        return "200";
    }
    //解封
    public String unBlock(String email){
        UpdateWrapper<UserModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("email",email).set("is_valid",1);
        userMapper.update(null,updateWrapper);
        return "200";
    }
}
